﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Microsoft.Web.Mvc;

namespace etcetera.Mvc.UI.Html
{
    public static class ImageLinkExtensions
    {
        /// <summary>
        /// Simple link wrapping an image.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="linkUrlAction">The link URL action.</param>
        /// <param name="imageUrlPath">The image URL path.</param>
        /// <param name="altText">The alt text.</param>
        /// <returns></returns>
        public static string ImageLink<T>(this HtmlHelper helper,
                                          Expression<Action<T>> linkUrlAction,
                                          string imageUrlPath,
                                          string altText) where T : Controller
        {
            return ImageLink(helper, linkUrlAction, imageUrlPath, altText, null);
        }

        /// <summary>
        /// Simple link wrapping an image.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="linkUrlAction">The link URL action.</param>
        /// <param name="imageUrlPath">The image URL path.</param>
        /// <param name="altText">The alt text.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static string ImageLink<T>(this HtmlHelper helper, 
                                          Expression<Action<T>> linkUrlAction, 
                                          string imageUrlPath, 
                                          string altText,
                                          object htmlAttributes) where T : Controller
        {
            string link;

            if (htmlAttributes != null)
            {
                link = helper.ActionLink(linkUrlAction, "{IMG}", htmlAttributes).ToString();
            }
            else
                link = helper.ActionLink(linkUrlAction, "{IMG}").ToString();

            //string image = helper.Image(imageUrlPath, altText);
            string image = string.Format("<img src=\"{0}\" alt=\"{1}\" border=\"0\" />", imageUrlPath, altText);

            string outputHtml = link.Replace("{IMG}", image);

            return outputHtml;
        }
    } 
}
